import { ref } from 'vue'
import { defineStore } from 'pinia'
import { useCartStore } from './cart'
import { getUserInfo } from '@/api/user.js'

export const useUserStore = defineStore(
  'userInfo',
  () => {
    const cartStore = useCartStore()
    const token = ref('')
    const setToken = (data) => (token.value = data)

    const userInfo = ref(false)
    const setUserInfo = async () => {
      const res = await getUserInfo()
      userInfo.value = res
    }

    const clearToken = () => {
      token.value = ''
      userInfo.value = ''
      cartStore.clearList()
    }

    return { token, userInfo, setUserInfo, setToken, clearToken }
  },
  {
    persist: true
  }
)
